package model.graphic.objects;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import model.graphic.locations.FlexibleRelativeLocation;
import model.graphic.locations.VerticalLineLocation;

/**
 *
 * @author Jelle , modified by Ben
 */
public class AbstractInPort extends LeftRectangleHandle {
    private boolean mustResize = false;

    public AbstractInPort() {
    }

    public AbstractInPort(RectangleGraphical baseNode, int minnSize, int width, int height) {
        super(baseNode, new VerticalLineLocation(new FlexibleRelativeLocation(baseNode.location, -width, 0), new FlexibleRelativeLocation(baseNode.location, 0, 0), new FlexibleRelativeLocation(baseNode.lowerLeft, 0, -15)), minnSize, width, height);
        this.width = width;
    }

    public void paint(Graphics2D g) {
        if (this.isVisible()) {
            if(mustResize){
                resizeWidth(g);
            }
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();
            Font oldFont = g.getFont();

            g.setColor(backColor);
            g.fillRect(location.getX(), location.getY(), getWidth(), getHeight());

            if (selected || highlighted) {
                g.setColor(highlightColor);
            } else {
                g.setColor(baseColor);
            }

            g.drawRect(location.getX(), location.getY(), getWidth(), getHeight());

            if (displayName != null && this.isVisible()) {
                g.setColor(Color.BLACK);
                drawCentered(g, displayName, new Rectangle(getUpperLeft().getX(), getUpperLeft().getY(), getWidth(), getHeight()));
            }

            g.setStroke(oldStroke);
            g.setColor(oldColor);
            g.setFont(oldFont);
        }
    }

    @Override
    public void setName(String name) {
        displayName = name;
        if(this.name.equals("NoName")){
           super.setName(name);
        }
        this.setToolTipText(name);
        mustResize = true;
    }

    private void resizeWidth(Graphics2D g){
        mustResize=false;
        g.setFont(new Font("Arial", Font.PLAIN, 11));
        FontMetrics fm = g.getFontMetrics();
        Rectangle2D textBounds = fm.getStringBounds( displayName, g);
        this.setWidth((int)textBounds.getWidth() + 2);
        RectangleGraphical rg = (RectangleGraphical) this.getBase();
        //fix the alignment to the base
        this.setLocation(new VerticalLineLocation(
                new FlexibleRelativeLocation(rg.getUpperLeft(), -width-1, this.getGraphLocation().getY() - rg.getGraphLocation().getY()),
        rg.getUpperLeft(), rg.getLowerRight()));
        this.setLocations(this.getGraphLocation());
    }

    public void collapse(boolean b) {
        RectangleGraphical rg = (RectangleGraphical) this.getBase();
        if (!b) {
            mustResize=true;
          /*  this.setLocation(new VerticalLineLocation(new FlexibleRelativeLocation(rg.getUpperLeft(), -41, this.getGraphLocation().getY() - rg.getGraphLocation().getY()),
                    rg.getUpperLeft(), rg.getLowerRight()));
            this.setLocations(this.getGraphLocation());*/
            setVisible(true);
            collapsed = false;
        } else {
            setWidth(0);
            this.setLocation(new VerticalLineLocation(new FlexibleRelativeLocation(rg.getUpperLeft(), -1, this.getGraphLocation().getY() - rg.getGraphLocation().getY()),
                    rg.getUpperLeft(), rg.getLowerRight()));
            this.setLocations(this.getGraphLocation());
            setVisible(false);
            collapsed = true;
        }
    }

}
